Unity_触摸屏_Unity物体和UI SetActive会触发的事件

刚开始游戏启动 游戏物体或者UI 就会触发一次 Awake事件 这个事件比Start触发早

触发顺序依次是?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Canvas2 : MonoBehaviour
{
    //刚开始触发一次
    private void Awake()
    {
        Debug.Log("我触发了Awake");
    }

    private void Start()
    {
        Debug.Log("我触发了Start");
    }

    //SetActive(true)时触发的事件
    private void OnEnable()
    {
        Debug.Log("我触发了OnEnable");
    }

    //SetActive(false)时触发的事件
    private void OnDisable()
    {
        Debug.Log("我触发了OnDisable");
    }
}

SetActive(true) 时触发OnEnable()事件

SetActive(false)时触发OnDisable()事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity_AR_Vuforia是一个基于Vuforia的AR应用开发引擎,可以方便地实现AR应用开发。在Unity_AR_Vuforia中,Virtual Button虚拟按钮可以用来触发一些交互操作,比如显示UI Image。下面是一个简单的示例,展示如何利用Virtual Button虚拟按钮显示UI Image。 首先,创建一个新的Unity项目,并导入Vuforia的AR插件。然后,在场景中添加一个ImageTarget,并将其设置为所需的图像识别目标。在ImageTarget下创建一个新的GameObject,并将其命名为VirtualButton。然后,将VirtualButton对象的Transform组件的位置设置在ImageTarget的中心位置。 接下来,在VirtualButton对象上添加一个VirtualButtonBehaviour组件。在VirtualButtonBehaviour组件中,将Virtual Button Type设置为Rectangle,并设置Size和Area。然后,在VirtualButtonBehaviour组件中选择需要触发事件,这里选择OnClick事件。 然后,在场景中创建一个UI Image对象,并将其作为VirtualButton对象的子对象。将UI Image对象的Transform组件的Scale属性设置为0,这样UI Image对象在场景中是不可见的。 最后,将下面的代码添加到场景中的一个脚本中,该脚本被添加到VirtualButton对象上。这个脚本将在Virtual Button被触发时显示UI Image对象。 ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Vuforia; public class VirtualButtonHandler : MonoBehaviour, IVirtualButtonEventHandler { public GameObject uiImage; void Start() { VirtualButtonBehaviour vbb = GetComponentInChildren<VirtualButtonBehaviour>(); vbb.RegisterEventHandler(this); uiImage.SetActive(false); } public void OnButtonPressed(VirtualButtonBehaviour vb) { uiImage.SetActive(true); } public void OnButtonReleased(VirtualButtonBehaviour vb) { uiImage.SetActive(false); } } ``` 这段代码中,VirtualButtonHandler脚本实现了IVirtualButtonEventHandler接口,这样它可以接收Virtual Button事件。在Start()方法中,VirtualButtonBehaviour对象被注册为Virtual Button事件的处理程序。在OnButtonPressed()方法中,显示UI Image对象。在OnButtonReleased()方法中,隐藏UI Image对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值